package com.mfanw.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;

import com.mfanw.fragment.Part1Fragment;
import com.mfanw.fragment.Part2Fragment;

public class MyFragmentActivity extends FragmentActivity {

	private FragmentManager fMgr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_fragment);
		initFragment();
		initButtonHandler();
	}

	private void initFragment() {
		fMgr = getSupportFragmentManager();
		FragmentTransaction ft = fMgr.beginTransaction();
		Part1Fragment part1 = new Part1Fragment();
		ft.add(R.id.fragmentRoot, part1);
		ft.commit();
	}

	private void initButtonHandler() {
		RadioButton btn1 = (RadioButton) findViewById(R.id.radio_part1);
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				FragmentTransaction ft = fMgr.beginTransaction();
				Part1Fragment part1 = new Part1Fragment();
				ft.replace(R.id.fragmentRoot, part1);
				ft.commit();
			}
		});
		RadioButton btn2 = (RadioButton) findViewById(R.id.radio_part2);
		btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				FragmentTransaction ft = fMgr.beginTransaction();
				Part2Fragment part2 = new Part2Fragment();
				ft.replace(R.id.fragmentRoot, part2);
				ft.commit();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.my_fragment, menu);
		return true;
	}
}
